
package com.deer.wms.base.system.service.MESWebService;

import com.alibaba.fastjson.JSONObject;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.GuidUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/out/billOutDetail")
public class BillOutDetailController{

    @Autowired
    private IBillOutDetailService billOutDetailService;

    @Autowired
    private ServerVisitAddressService serverVisitAddressService;


    //提供给WCS请求的接口并返回相应数据
    @GetMapping("/supplyWCSTransfer")
    @ResponseBody
    public Result selectInventory(Integer i) {
        //传入参数之后看用什么方式进行处理再决定改为什么
        WebserviceResponse webserviceResponse = null;
        try {
            //获取WCS的传递过来的箱号（测试时此处可以任意赋值）
            //此处  i  为WCS传递类型可以暂时不管，之后看WCS传什么值再把布尔表达式修改为相应的信息selectInventory(
            if(i==0) {
                //当i为0时调用入库工单信息请求接口
                String barCode = "";
                //调用接口
               webserviceResponse = WipInforReq(barCode);
                //处理MES返回的信息,（目前不知道是否是在webServiceResponse的resultData中返回）如果成功应该是要去条码系统查询对应的信息
//                if(true){
//
//                }else{
//
//                }

            }else if(i==1){
                //空框到达立体库出料口
                //空框到立体库出料口扫描到的的箱号
                String barCode = "";
                webserviceResponse = EmptyShelfArrive(barCode);
                //根据MES返回的信息处理逻辑
//                if(true){
//
//                }else{
//
//                }
            }else if(i==2){
                //3.工单退库请求
                //退库箱号
                String barCode = "";
                webserviceResponse = RetWipReq(barCode);
                //根据MES返回的信息处理逻辑
//                if(true){
//
//                }else{
//
//                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR);
        }finally{

        }
        return ResultGenerator.genSuccessResult("OK");
    }

    //入库工单信息请求接口
    private WebserviceResponse WipInforReq(String barCode) {
        WebserviceResponse webserviceResponse =null;
        try {
            //设备地址
            String macCode = "";
            //WCS扫描箱子上的二维码并返回到WMS传给
//            berthCode;

            String code = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "\n" +
                    "<WipInforReq\n" +
                    "\n" +
                    "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                    "\n" +
                    "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
                    "\n" +
                    "macCode=\""+ macCode +"\"\n" +
                    "\n" +
                    "barCode=\""+barCode+"\"\n" +
                    "\n" +
                    "taskCode=\""+new GuidUtils().toString()+"\">\n" +
                    "\n" +
                    "</WipInforReq>";
            webserviceResponse = serverVisitAddressService.requestMesServerThree("WipInforReq",code);
        } catch (Exception e) {
            e.printStackTrace();
            webserviceResponse = new WebserviceResponse(null,"-1","调用WMS接口出错",null);
        }
        return webserviceResponse;
    }


    /**
     * 空框到达立体库出料口
     * @return
     */
    private WebserviceResponse EmptyShelfArrive(String barCode) {
        WebserviceResponse webserviceResponse =null;
        try {
            //设备编码
            String macCode = "";
            //设备地址码
            String berthCode = "";
            String code = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "\n" +
                    "<EmptyShelfArrive\n" +
                    "\n" +
                    "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                    "\n" +
                    "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
                    "\n" +
                    "macCode=\""+macCode+"\"\n" +
                    "\n" +
                    "barCode=\""+barCode+"\"\n" +
                    "\n" +
                    "berthCode=\""+berthCode+"\"\n" +
                    "\n" +
                    "taskCode=\""+new GuidUtils().toString()+"\">\n" +
                    "\n" +
                    "</EmptyShelfArrive>";

            webserviceResponse = serverVisitAddressService.requestMesServerThree("EmptyShelfArrive",code);
        } catch (Exception e) {
            e.printStackTrace();
            webserviceResponse = new WebserviceResponse(null,"-1","调用WMS接口出错",null);
        }
        return webserviceResponse;
    }
    //RetWipReq
    private WebserviceResponse RetWipReq(String barCode) {
        WebserviceResponse webserviceResponse =null;
        try {
            //设备编码
            String macCode = "";
            //设备地址码
            String berthCode = "";
            String code = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "\n" +
                    "<RetWipReq\n" +
                    "\n" +
                    "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                    "\n" +
                    "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
                    "\n" +
                    "macCode=\""+macCode+"\"\n" +
                    "\n" +
                    "barCode=\""+barCode+"\"\n" +
                    "\n" +
                    "berthCode=\""+berthCode+"\"\n" +
                    "\n" +
                    "taskCode=\""+new GuidUtils().toString()+"\">\n" +
                    "\n" +
                    "</RetWipReq>";

            webserviceResponse = serverVisitAddressService.requestMesServerThree("RetWipReq",code);
        } catch (Exception e) {
            e.printStackTrace();
            webserviceResponse = new WebserviceResponse(null,"-1","调用WMS接口出错",null);
        }
        return webserviceResponse;
    }
}
